package com.msmall.community.mood.action.html;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.community.mood.model.Mood;
import com.msmall.community.mood.service.MoodManager;

public class MoodAction extends HTMLBaseAction{
	
	private final Log logger = LogFactory.getLog(MoodAction.class);
	private static final long serialVersionUID = -2777000017703505410L;
	
	private String mood;
	
	private MoodManager moodManager;

	/**
	 * 暂时未用；可用来查看用户所有心情的记录
	 */
	public String execute(){
		return SUCCESS;
	}

	/**
	 * 保存用户心情;在用户个人首页AJAX保存
	 * @return
	 */
	public String save(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		Mood moodx = new Mood();
		moodx.setMood(mood);
		try {
			moodManager.saveMood(moodx, getUserid());
			buffer.append(createResult("true"));
		} catch (Exception e) {
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	public String list(){
		try{
			if(usr == null || usr == 0){
				if(!getIsLogin()){
					return ERROR;
				}else{
					usr = getUserid(); // 如果是登陆用户，跳转到自己的首页
				}
			}
			if(usr.compareTo(getUserid()) == 0){
				isOwner = true;
			}
			page.setPageSize(10);
			if(isOwner){
				moodManager.getFriendMood(page, usr);
			}else{
				moodManager.getPagination(page, usr);
			}
		}catch(Exception e){
			logger.error(e);
			return ERROR;
		}
		return SUCCESS;
	}
	public String getMood() {
		return mood;
	}

	public void setMood(String mood) {
		this.mood = mood;
	}

	public void setMoodManager(MoodManager moodManager) {
		this.moodManager = moodManager;
	}
}
